

#ifndef WIN32
#include <kos.h>
#define DBGOUT(string) printf(string);
#define DBGOUTP(string,x) printf(string,x);
#define O_BINARY 0
#endif

#ifdef WIN32
#include "../koswin/koswin.h"
#endif


#include "agm.h"
#include <GL/gl.h>


#define AGM_VERSION 2
#define AGM_MAGIC (('A') | ('G'<<8) | ('M'<<16) | ('1'<<24))
#define XFRM_MAGIC (('X') | ('F'<<8) | ('R'<<16) | ('M'<<24))
#define FRAM_MAGIC (('F') | ('R'<<8) | ('A'<<16) | ('M'<<24))


int load_agm(char *filename, agmmodel *obj)
{
	uint32 file;
	unsigned int cnt,i,j;
	agmVtx tempVtx;
	agmXForm tempXForm;
	AGMHeader agm_header;
//	char string[100];

//	printf("loading %s...",filename);
//	MessageBox(NULL,"Loading AGM","DBG",MB_OK);

	i=0;
	obj->Vtx=0;
 

	file = fs_open(filename, O_RDONLY|O_BINARY);

	if (file==-1) {
		DBGOUT("Error Loading AGM");
		return 1;
	}
	

	// load header
	cnt = fs_read(file, (void*)&agm_header, sizeof(AGMHeader));
	if (agm_header.ident!=AGM_MAGIC) {
		DBGOUT("not AGM file");
		return 1;
	}
	if (agm_header.version!=AGM_VERSION) {
		DBGOUT("incorrect AGM version");
		return 1;
	}
	obj->numVtx = agm_header.numVtx;
	obj->numFrame = agm_header.numFrame;
	obj->curFrame = 0;


	//load xform
	cnt = fs_read(file, &i, sizeof(i));//read magic
	if (i!=XFRM_MAGIC) {
		DBGOUT("AGM XForm Unaligned");
		return 1;
	}
	cnt = fs_read(file, &tempXForm, sizeof(tempXForm));
	obj->scale.x=tempXForm.sx;
	obj->scale.y=tempXForm.sy;
	obj->scale.z=tempXForm.sz;
	obj->trans.x=tempXForm.tx;
	obj->trans.y=tempXForm.ty;
	obj->trans.z=tempXForm.tz;
	obj->transtx.x=tempXForm.ttu;
	obj->transtx.y=tempXForm.ttv;
	obj->scaletx.x=tempXForm.stu;
	obj->scaletx.y=tempXForm.stv;

	obj->Vtx = (agmVtx*)malloc(agm_header.numVtx * sizeof(agmVtx) * agm_header.numFrame);
	//read Vtx
	for (j=0;j<agm_header.numFrame;j++) {
		cnt = fs_read(file, &i, sizeof(i));//read magic
		if (i!=FRAM_MAGIC) {
			DBGOUT("AGM Frame Unaligned");
			fs_close(file);
			return 1;
		}
		for (i=0;i<agm_header.numVtx;i++) {
			cnt = fs_read(file, &tempVtx, sizeof(tempVtx));
			obj->Vtx[i+j*agm_header.numVtx].x=tempVtx.x;
			obj->Vtx[i+j*agm_header.numVtx].y=tempVtx.y;
			obj->Vtx[i+j*agm_header.numVtx].z=tempVtx.z;
			obj->Vtx[i+j*agm_header.numVtx].u=tempVtx.u;
			obj->Vtx[i+j*agm_header.numVtx].v=tempVtx.v;
		}
	}

	obj->currVtx=obj->Vtx;

	fs_close(file);

//	printf("loaded\r\n");
//	MessageBox(NULL,"Loaded AGM","OK",MB_OK);
//	sprintf(string,"numvtx:%i",obj->numVtx);
//	MessageBox(NULL,string,"OK",MB_OK);

	return 0;
}

void draw_agm(agmmodel *agm_model) 
{
	unsigned short i;
	glBegin(GL_TRIANGLES);
		for (i=0;i<agm_model->numVtx;i++)
		{
			glTexCoord2f(((float)agm_model->currVtx[i].u)*agm_model->scaletx.x+agm_model->transtx.x, ((float)agm_model->currVtx[i].v)*agm_model->scaletx.y+agm_model->transtx.y);
	//		glNormal3f(agm_model->norms[agm_model->tris[i]].nx, agm_model->norms[agm_model->tris[i]].ny, agm_model->norms[agm_model->tris[i]].nz);
			glVertex3f(((float)agm_model->currVtx[i].x)*agm_model->scale.x+agm_model->trans.x
				, ((float)agm_model->currVtx[i].y)*agm_model->scale.y+agm_model->trans.y
				, ((float)agm_model->currVtx[i].z)*agm_model->scale.z+agm_model->trans.z);
		}
	glEnd();
}

void UpdateAnim(agmmodel *agm_model) 
{
	unsigned int curframe=(int)agm_model->curFrame;
	if (agm_model->curFrame>=agm_model->numFrame) curframe=agm_model->numFrame-1;
	agm_model->currVtx=&(agm_model->Vtx[curframe*agm_model->numVtx]);
}

void free_agm(agmmodel *agm_model) 
{
	if (agm_model->Vtx) free(agm_model->Vtx);
	agm_model->Vtx=NULL;
}
